package hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Author:xuyuanhong
 * @Date:Created in 2022/3/7 15:51
 */
public class Wordcount {

    // 实现main 函数： psvm
    public String wc(String srcPath,String destPath )  throws IOException, ClassNotFoundException, InterruptedException{
        // 设置 hdfs的配置
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        // 设置 jar包 加载路径
        job.setJarByClass(Wordcount.class);
        // 设置 map和reduce 的类
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReduce.class);
        // 设置 map端输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 设置输出的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 设置下 数据输入和数据的参数
        FileInputFormat.setInputPaths(job,new Path(srcPath));
        FileOutputFormat.setOutputPath(job,new Path(destPath));
        // 提交
        boolean result = job.waitForCompletion(true);
        System.out.println(result? 0:1);

        return "执行完毕";
    }

}
